package com.example.holes;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;

/*
 * Hole handles the graphics and logic of the hole. Hole are numbers left-to-right
 * bottom-to-top 1-9. Specifying the hole # in the constructor supplies the
 * corresponding graphic.
 */
public class Hole {
	Bitmap[] images; // arraylist with all loaded hole images
	private int IMAGES_LENGTH; // length of images array
	private int left, top, row;
	private int holeNum;
	private Bitmap image;
	private boolean full = false; // from opening to closed
	private boolean opening = false;
	private boolean closing = false;
	private int TIME_BETWEEN = 2;
	private int animateTimer = TIME_BETWEEN;
	private int frame = 0;
	private Mole mole; // each hole contains a mole
	private boolean hasMole = false; // if mole has spawned
	private Loot loot; // each hole contains loot
	private boolean hasLoot = false; // if loot in hole

	public Hole(int holeNum, Bitmap[] images){
		this.images = images;
		IMAGES_LENGTH = (int)((float)images.length / 3);
		
		this.holeNum = holeNum;
		setImagePosition(holeNum);
		image = getImage(frame);
		
		// scale position for different screen sizes
		left = (int)(Global.SCALE_RATIO_W * left);
		top = (int)(Global.SCALE_RATIO_H * top);
	}
	
	// gives the position of the hole's image from input number
	private void setImagePosition(int holeNum){
		switch (holeNum) {
			case 0:	row = 1;
					left = 139;
					top = 352;
					break;
			case 1:	row = 1;
					left = 320;
					top = 352;
					break;
			case 2:	row = 1;
					left = 501;
					top = 352;
					break;
			case 3:	row = 2;
					left = 173;
					top = 236;
					break;
			case 4:	row = 2;
					left = 332;
					top = 236;
					break;
			case 5:	row = 2;
					left = 491;
					top = 236;
					break;
			case 6:	row = 3;
					left = 200;
					top = 155;
					break;
			case 7:	row = 3;
					left = 343;
					top = 155;
					break;
			case 8:	row = 3;
					left = 486;
					top = 155;
					break;
		}
	}

	// ==============
	//     UPDATE
	// ==============
	// checks if the hole should be open/close animating
	public void Update(){
		if (opening){
			openingAnimation(); // starts mole at end
		}
		else if (closing){
			closingAnimation();
		}
	}

	// hole considered full, starts opening animation
	public void fill(Mole mole){
		if (!full){
			opening = true;
			full = true;
			this.mole = mole;
		}
	}

	// starts closing animation.
	// hole not considered empty until animation is complete
	public void close(){
		if (full){
			closing = true; // begin closing animation
		}
	}
	
	// kill mole in hole. 
	public void removeMole(){
		mole = null; // mole killed
		hasMole = false;
	}
	
	// add loot to hole.
	public void addLoot(Loot loot){
		hasLoot = true;
		this.loot = loot;
	}
	
	// removes loot and starts closing hole
	public void removeLoot(){
		hasLoot = false;
		loot = null;
		close();
	}
	
	public void reset(){
		loot = null;
		hasLoot = false;
		mole = null;
		hasMole = false;
		full = false;
		opening = false;
		closing = false;
		frame = 0;
		image = getImage(frame);
		animateTimer = TIME_BETWEEN;
	}

	// ==============
	//     DRAW
	// ==============
	// draw the hole
	public void Draw(Canvas c){
		c.drawBitmap(image, left, top, null);
	}

	// animates hole opening, once fully open stops animating
	// mole then exists
	public void openingAnimation(){
		animateTimer--;
		if (animateTimer == 0){
			frame++;
			image = getImage(frame);
			animateTimer = TIME_BETWEEN;
			if (frame == 5){
				opening = false; // hole opened
				hasMole = true; // mole now exists
				mole.start();
			}
		}
	}

	// animates hole closing, once closed, sets full to false
	public void closingAnimation(){
		animateTimer--;
		if (animateTimer == 0){
			frame--;
			image = getImage(frame);
			animateTimer = TIME_BETWEEN;
			if (frame == 0){
				closing = false;
				full = false;
			}
		}
	}

	// returns corresponding image from preloaded iamges
	public Bitmap getImage(int index){
		index = index + ((row - 1) * IMAGES_LENGTH);
		return images[index];
	}

	// getters
	public boolean isFull(){ return full; }
	public boolean hasMole(){ return hasMole; }
	public boolean hasLoot(){ return hasLoot; }
	public Mole getMole(){ return mole; }
	public Loot getLoot(){ return loot; }
	public int getNum(){ return holeNum; }
}
